// deepminez.cpp : Defines the entry point for the console application.
//


#include <conio.h>


#include <windows.h>

#include "common.h"
#include "GameEngine.h"
#include "GlobalGameData.h"

 #pragma warning(disable : 4996)


// main game object
GameEngine theGame;

// object that contains global data such as:
//  - pointer to the world object
//  - game settings
GlobalGameData gameData;

HANDLE output = INVALID_HANDLE_VALUE;

void gotoxy(int x, int y) 
{ 
	COORD pos = {x, y};
	SetConsoleCursorPosition(output, pos);
}

inline void setcol(short col)
{
	SetConsoleTextAttribute(output, col);
}

inline void puttile(short col, wchar_t ch)
{
	setcol(col);
	putwchar(ch);
}

int main(int argc, wchar_t* args[])
{
	
	wprintf(L"Loading...");
	theGame.Init();
	
	output = GetStdHandle(STD_OUTPUT_HANDLE);

	int cols = theGame.m_world.m_map.GetCols();
	int rows = theGame.m_world.m_map.GetRows();
	int depth = theGame.m_world.m_map.GetDepth();

	int wx1,wy1,wx2,wy2,wz;
	wx1=wy1=0;
	wx2=wy2=20;
	wz=25;

	wchar_t* line = new wchar_t[wx2-wx1+1];

	bool work=true;
	while(work)
	{
		theGame.MainCycle();

		if(kbhit())
		{
			int key = getch();
			theGame.OnKeyDown(key);
			theGame.OnKeyUp(key);

			switch(key)
			{
			case 'a':
				if(wx1>0)
				{ wx1--;wx2--;}
				break;
			case 'd':
				if(wx2<cols)
				{ wx1++;wx2++;}
				break;
			case 'w':
				if(wy1>0)
				{ wy1--;wy2--;}
				break;
			case 's':
				if(wy2<cols)
				{ wy1++;wy2++;}
				break;
			case 'q':
				if(wz<depth-1)
				{
					wz++;
				}
				break;
			case 'z':
				if(wz>0)
				{
					wz--;
				}
				break;
			case ' ':
				break;
			case 27:
				work=false;
				break;
			}
		}

		
		gotoxy(0,0);
		for(int y=wy1;y<wy2;y++)
		{
			
			for(int x=wx1;x<wx2;x++)
			{
				//int pos = x-wx1;
				if(y>=rows || x>=cols)
				{
					puttile(0x0f,'.');
				}
				else
				{
					DCell* cell = theGame.m_world.m_map.GetCellAt(wz,y,x);
					if(cell->m_fillMat==NULL)
					{
						if(cell->m_creature!=NULL)
						{
							puttile(cell->m_creature->m_color,cell->m_creature->m_code);
						}
						else if(cell->m_mainObj!=NULL)
						{
							puttile(cell->m_mainObj->m_color,cell->m_mainObj->m_code);
						}
						else if(cell->m_floorMat==NULL)
						{
							if(wz>0)
							{
								DCell* bottom = theGame.m_world.m_map.GetCellAt(wz-1,y,x);
								if(bottom->m_fillMat!=NULL)
									puttile(bottom->m_fillMat->m_color,177);
								else if(bottom->m_floorMat!=NULL)
									puttile(bottom->m_floorMat->m_color,176);
								else
									puttile(0x00,' ');
							}
							else
								puttile(0x00,' ');
						}
						else
							puttile(cell->m_floorMat->m_color,178);
					}
					else
					{
						puttile(cell->m_fillMat->m_color,cell->m_fillMat->m_code);
					}
				}
				
			}
			putwchar('\n');
			//line[wy2-wy1]='\0';
			//_putws(line);
			
		}
		setcol(0x0f);
		wprintf(L"x: %d, y: %d, z: %d\n",wx1,wy1,wz);
		DDwarf* dwarf1 = (DDwarf*)theGame.m_world.m_dwarves.front();
		wprintf(L"hunger: %0.2f, task: %d",dwarf1->m_state.hunger,dwarf1->m_task->m_curTask);


		Sleep(5);
		
	}

	gotoxy(0,0);
	wprintf(L"Exiting...  ");
	theGame.Deinit();
	return 0;
}

